package com.putaoshu.ptp;


import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

@Component
public class P2pPointServer {
private Logger logger = LoggerFactory.getLogger(P2pPointServer.class);
private int port = 7001;
private List<WebSocket> localSockets =  new ArrayList<WebSocket>();
public List<WebSocket> getLocalSockets(){
    return localSockets;
}
public void setLocalSockets(List<WebSocket> localSockets)
{
    this.localSockets = localSockets;
}

@PostConstruct
    @Order(1)
    public void initServer()
{
    final WebSocketServer socketServer  = new WebSocketServer(new InetSocketAddress(port)) {
        @Override
        public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
        sendMessage(webSocket,"北京服务端成功创建连接");
        localSockets.add(webSocket);
        }

        @Override
        public void onClose(WebSocket webSocket, int i, String s, boolean b) {
        logger.info(webSocket.getRemoteSocketAddress()+"客户端与服务端断开连接！");
        localSockets.remove(webSocket);
        }

        @Override
        public void onMessage(WebSocket webSocket, String s) {
            logger.info("收到消息"+s);
            sendMessage(webSocket,"收到消息");
        }

        @Override
        public void onError(WebSocket webSocket, Exception e) {
            logger.info(webSocket.getRemoteSocketAddress()+"客户端与服务端发生错误！");
            localSockets.remove(webSocket);
        }

        @Override
        public void onStart() {
        logger.info("北京的websocket server启动....");
        }
    };
    socketServer.start();
    logger.info("北京服务端监听socketServer端口："+port);

}
    public void sendMessage(WebSocket ws,String message)
    {
        logger.info("发送给"+ws.getRemoteSocketAddress().getPort()+"的消息是"+message);
    }
    //向所有连接到本机的客户端发送消息

    public void broadcast(String message)
    {
            if(localSockets.size()==0|| StringUtils.isEmpty(message))
            {
                return;
            }
            logger.info("Glad to say broadcast to clients being started");
            for(WebSocket socket:localSockets)
            {
                this.sendMessage(socket,message);
            }
            logger.info("Glad to say braodcast to clients has  overred");
    }
}
